package mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.api.ApiService;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.BaseChannelItem;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.MessageE;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.SearchBean;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.AdapterlistBaseChannelItem;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.SearchRecyAdapter;
import mipushdemo.xiaomi.com.diyitonghang.utils.RetrofitUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class SearchFragment extends Fragment {

    private String name;
    private RecyclerView ryChannel;
    private String title;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle arguments = getArguments();
        if (arguments != null) {
            name = arguments.getString("name");
            title = arguments.getString("title");
        }
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = null;
        if (name.equals("文章")) {
            inflate = LayoutInflater.from(this.getActivity()).inflate(R.layout.layout_bacechannel, null);
            initView1(SearchDataActivity.title);
            ryChannel = (RecyclerView) inflate.findViewById(R.id.ryChannel);

        }
        return inflate;
    }


    public static SearchFragment getFragment(String name,String title) {
        SearchFragment search = new SearchFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putString("title",title);
        search.setArguments(bundle);
        return search;
    }
    private void initView1(String title) {
        //拼接json字符串
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("keyword",SearchDataActivity.title);
            jsonObject.put("cursor", "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String s = jsonObject.toString();
        String header = "application/json";
        RequestBody requestBody = RequestBody.create(MediaType.parse(header), s);
        ApiService apiService = RetrofitUtils.newInstance().netWorkRequest();
        Observable<SearchBean> observable = apiService.newsSearch(header, requestBody);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SearchBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SearchBean searchBean) {
                        final List<SearchBean.DataBean.NewListBean> newList = searchBean.getData().getNewList();
                        SearchRecyAdapter adapter = new SearchRecyAdapter(newList);
                        ryChannel.setLayoutManager(new LinearLayoutManager(getContext()));
                        ryChannel.setAdapter(adapter);
                        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                Intent intent = new Intent(getContext(), InfoActivity.class);
                                intent.putExtra("newsId",newList.get(position).getNewsId());
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
